package com.pro.framework.api.util;

import cn.hutool.json.JSONUtil;

import java.util.*;

public class UrlUtil {
    public static final List<String> props = Arrays.asList("state", "userId", "subAccountId", "extendId", "reAuth", "reAuth2");

    public static void main(String[] args) {
//        String state = "a";
//        String userId = null;
//        String subAccountId = "c";
//        String extendId = "";
//        String reAuth = "e";
//        String reAuth2 = null;
//
//        String url = appendProp("_", props, state, userId, subAccountId, extendId, reAuth, reAuth2);
//        System.out.println("拼接结果: " + url);

        List<String> splits = splitProp("_", props, null);
        System.out.println("还原结果: " + splits);
    }

    /**
     * 拼接成字符串，中间用双下划线 __ 分隔
     */
    public static String appendProp(String delimiter, List<String> props, Object... values) {
        if (values == null || props == null || props.size() != values.length) {
            throw new IllegalArgumentException("参数数量与 props 不一致");
        }
        List<String> encoded = new ArrayList<>();
        for (Object value : values) {
            encoded.add(value == null ? "" : value.toString());
        }
        return String.join(delimiter, encoded);
    }

    /**
     * 拆分字符串，还原成按 props 顺序排列的值（长度与 props 相同）
     */
    public static List<String> splitProp(String delimiter, List<String> props, String str) {
        if (props == null || str == null) return Collections.emptyList();
        String[] parts = str.split(delimiter, -1); // -1 保留尾部空串
        List<String> result = new ArrayList<>();
        for (int i = 0; i < props.size(); i++) {
            result.add(i < parts.length ? parts[i] : null);
        }
        int sub = props.size() - result.size();
        for (int i = 0; i < sub; i++) {
            result.add(null);
        }
        return result;
    }

}
